package AdminBD;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import adminHibernate.HibernateConnection;
import entidadesHibernate.Titular;

public class DAOTitularMySql implements DAOTitular {
	
	public Titular buscarTitular(String tipoDoc, String numDoc) {
		Session session = HibernateConnection.conectar();	
		String hql = "from Titular where tipoDoc = '" + tipoDoc + "' and dni = '" + numDoc + "'";
		
		Titular titular = new Titular();
		
		try { 
			
			session.beginTransaction();			
			Query query = session.createQuery(hql);
			titular = (Titular) query.uniqueResult();
			session.getTransaction().commit();							
			
			}
		catch(HibernateException e) {
		       System.out.println(e);
		     } 
		finally { 
			session.close();
			}
		
		return titular;
	}

	public void guardarTitular(Titular titular) {
		
		Session session = HibernateConnection.conectar();
		
		try { 
			
			session.beginTransaction();			
			session.save(titular);										
			session.getTransaction().commit();	
			
			}
		catch(HibernateException e) {
		       System.out.println(e);
		     } 
		finally { 
			session.close();
		}		
	}

}
